/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.checker.type;


/**
 * A dynamic array type.  Dynamic arrays are always stored
 * on the heap.  The "dynamic" in "dynamic array" refers to the
 * array's size, which is determined at runtime.  A static array,
 * in contrast, has its size fixed at compile time.
 */
final public class ArrayType extends Type {
	

	/**
	 * The element type of this array type.
	 */
	private Type elementType;

	
	/**
	 * Constructs a new dynamic array type occurrence.
	 * 
	 * @param elementType  The member type of the array type
	 */
	ArrayType(TypeManager m, Type elementType) {
		super(m);
		if (elementType == null) {
			throw new IllegalArgumentException();
		}
		this.elementType = elementType;
	}

	
	/**
	 * Returns false since dynamic arrays are always stored on the heap.
	 * 
	 * @return false, always
	 */
	@Override
	public boolean isLocal() {
		return false;
	}

	
	@Override
	public String toString() {
		return elementType + "[]";
	}


	/**
	 * Returns true since this type is not primitive.
	 * 
	 * @return true, always
	 */
	@Override
	public boolean isCompound() {
		return true;
	}

	
	/**
	 * Returns the element type of this array type.
	 * 
	 * @return  the element type of this array type
	 */
	public Type getElementType() {
		return elementType;
	}


	@Override
	public boolean canAssignTo(Type formal) {
		if (elementType.isLocal()) {
			return this.equals(formal);
		}
		if (!(formal instanceof ArrayType)) {
			return false;
		}
		ArrayType formalArray = (ArrayType)formal;
		Type formalElement = formalArray.getElementType();
		return elementType.canAssignTo(formalElement);		
	}

}
